/**
 * Created by YX on 2016/10/23.
 *
 */

function addHealthIndex() {
    var memberId = $("#memberId").val()
    $("#health_index tbody").append(

        '<tr>'+
        '<td style="display: none"></td>'+
        '<td style="display: none">'+memberId+'</td>'+
        '<td style="display: none">0</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '<td>请双击输入内容</td>'+
        '</tr>'

    );
}

function submitHealthIndex() {

    var healthIndexs=[];
    var checkDate = true;
    var checkNum = true;

    $("#health_index tbody tr").each(
        function () {
            var tds = $(this).find("td");
            if (tds[2].innerText=='1'){
                var healthIndex = {};


                healthIndex.id=tds[0].innerText;
                healthIndex.memberId=tds[1].innerText;
                healthIndex.logdayStr=tds[3].innerText=='请双击输入内容'?'':tds[3].innerText;
                healthIndex.bloodPressure_L = tds[4].innerText=='请双击输入内容'?'':tds[4].innerText;
                healthIndex.bloodPressure_H = tds[5].innerText=='请双击输入内容'?'':tds[5].innerText;
                healthIndex.pulse = tds[6].innerText=='请双击输入内容'?'':tds[6].innerText;
                healthIndex.bloodSugger = tds[7].innerText=='请双击输入内容'?'':tds[7].innerText;
                healthIndex.bloodFatTC = tds[8].innerText=='请双击输入内容'?'':tds[7].innerText
                healthIndex.bloodFatTG = tds[9].innerText=='请双击输入内容'?'':tds[9].innerText;
                healthIndex.urineSugar = tds[10].innerText=='请双击输入内容'?'':tds[10].innerText;
                healthIndex.urineProtein = tds[11].innerText=='请双击输入内容'?'':tds[11].innerText;
                healthIndexs.push(healthIndex);
                if (!checkDatef(healthIndex.logdayStr)){
                    checkDate=false;
                    return checkDate;
                }
                if(healthIndex.bloodPressure_L!=''&&!checkNumf(healthIndex.bloodPressure_L)){
                    checkNum=false;
                    alert('血压必须为数字');
                    return false;
                }
                if(healthIndex.bloodPressure_H!=''&&!checkNumf(healthIndex.bloodPressure_H)){
                    checkNum=false;
                    alert('血压必须为数字');
                    return false;
                }
                if(healthIndex.pulse!=''&&!checkNumf(healthIndex.pulse)){
                    checkNum=false;
                    alert('脉搏必须为数字');
                    return false;
                }

                if(healthIndex.bloodSugger!=''&&!checkNumf(healthIndex.bloodSugger)){
                    checkNum=false;
                    alert('血糖必须为数字');
                    return false;
                }

                if(healthIndex.bloodFatTC!=''&&!checkNumf(healthIndex.bloodFatTC)){
                    checkNum=false;
                    alert('血脂(总胆固醇)必须为数字');
                    return false;
                }

                if(healthIndex.bloodFatTG!=''&&!checkNumf(healthIndex.bloodFatTG)){
                    checkNum=false;
                    alert('血脂(甘油三酯)必须为数字');
                    return false;
                }



            }
        }
    );
    if (healthIndexs.length==0){
        alert("无新增有效更改");
        return;
    }

    if (checkDate && checkNum){
        $.ajax({
            type: 'POST',
            url: "/member/addOrUpdateHealthIndex",
            contentType:"application/json",
            data: JSON.stringify(healthIndexs),
            success: function (result) {
                alert("提交成功");

                $("#health_index tbody tr").each(
                    function () {
                        var tds = $(this).find("td");
                        tds[2].innerText=='0';
                    }
                );
            },
            dataType: "text"
        });
    }
    else if (!checkDate){
        alert("日期格式错误，请修改后再提交");
    }

}


function addMedical() {
    $("#medical tbody").append(

    '<tr>'+
    '<td style="display: none"></td>'+
    '<td style="display: none">'+$("#memberId").val()+'</td>'+
    '<td style="display: none">0</td>'+
    '<td>请双击输入内容</td>'+
    '<td>请双击输入内容</td>'+
    '<td>请双击输入内容</td>'+
    '<td>请双击输入内容</td>'+
    '<td>请双击输入内容</td>'+
    '</tr>'

    );
}

function submitMedical() {

    var medicals=[];

    $("#medical tbody tr").each(
        function () {
            var tds = $(this).find("td");
            if (tds[2].innerText=='1'){
                    var medical = {};

                    medical.id=tds[0].innerText;
                    medical.memberId=tds[1].innerText;
                    medical.medicalName=tds[3].innerText;
                    medical.dosage = tds[4].innerText;
                    medical.startDay = tds[5].innerText;
                    medical.endDay = tds[6].innerText;
                    medical.target = tds[7].innerText;
                    medicals.push(medical)
            }
        }
    );
    if (medicals.length==0){
        alert("无新增有效更改。");
        return;
    }

    $.ajax({
        type: 'POST',
        url: "/member/addOrUpdateMedical",
        contentType:"application/json",
        data: JSON.stringify(medicals),
        success: function (result) {
            alert("提交成功");
            $("#medical tbody tr").each(
                function () {
                    var tds = $(this).find("td");
                    tds[2].innerText=='0';

                }
            );
        },
        dataType: "text"
    });

}

function checkDatef(date){
    if (date==null){
        return false;
    }
    var reg=/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
    if(!date.match(reg)){
        return false;
    }else{
        return true;
    }

}

function checkNumf(num) {

    if (num == null)
        return false;
    else {
        return /^[0-9]+.?[0-9]*$/.test(num);
    }

}

function chengeMemberHealth() {

    var memberId = $("#selectMemberId").val();


        $("#main_context").empty();
        $("#main_context").load("/member/memberHealthIndex?memberId="+memberId);

    
}

function editDiseasesHistory() {
    $("#diseasesHistory").removeAttr("disabled");
}


function updateDiseasesHistory() {

    var params = {};

    params.memberId = $("#selectMemberId").val();
    params.diseasesHistory=$("#diseasesHistory").val();


    $.ajax({
        type: 'POST',
        url: "/member/updateMember",
        contentType:"application/json",
        data: JSON.stringify(params),
        success: function (result) {
            alert("更新信息成功");
        },
        dataType: "text"
    });

}

function changeMemberForHealthDisease() {
    var memberId = $("#selectMemberId").val();

    $("#main_context").empty();
    $("#main_context").load("/member/healthDiseaseRecordIndex?memberId="+memberId);


}
